export const isEmptyObj = (data: any) => {
  return JSON.stringify(data) == "{}";
};

export const getStorage = (name: string) => {
  let data = "";
  if (localStorage.getItem(name)) {
    data = localStorage.getItem(name)! ?? "";
  } else if (sessionStorage.getItem(name)) {
    data = sessionStorage.getItem(name)! ?? "";
  }
  return data;
};
export const setStorage = (key: string, name: string) => {
  sessionStorage.setItem(key, JSON.stringify(name));
};

// FileReader && Blob/File转化
// FileReader 对象允许web应用程序异步读取存储在用户计算机上的文件内容
export const readBlob = (blob, type) => {
  return new Promise((resolve) => {
    let reader = new FileReader();
    reader.onload = (event) => {
      resolve(event.target.result);
    };
    switch (type) {
      case "ArrayBuffer":
        // 生成ArrayBUffer
        reader.readAsArrayBuffer(blob);
        break;
      case "DataUrl":
        // 生成base64
        reader.readAsDataURL(blob);
        break;
      case "Text":
        reader.readAsText(blob, "utf-8");
        break;
      default:
        break;
    }
  });
};
// 将base64转为blob
export function base64toBlob(base64) {
  let bstr = window.atob(base64), // 获得base64解码后的字符串
    n = bstr.length,
    u8arr = new Uint8Array(n); // 新建一个8位的整数类型数组，用来存放ASCII编码的字符串
  while (n--) {
    u8arr[n] = bstr.charCodeAt(n); // 转换编码后才使用charCodeAt 找到Unicode编码
  }
  const fileStream = new Blob([u8arr], { type: "text/plain" });
  const url = URL.createObjectURL(fileStream);

  return url;
}
